package com.ro0tsh3ll.controller;

import com.ro0tsh3ll.model.GoogleIP;
import com.ro0tsh3ll.service.KickGoogleService;
import com.ro0tsh3ll.service.UserService;
import com.ro0tsh3ll.tools.SocketUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.sql.Connection;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2014/6/13.
 */
@Controller
@RequestMapping("/google")
public class KickGoogleController {

    @Autowired
    KickGoogleService kickGoogleService;


    @RequestMapping("search")
    public String search(){

        return "google/index";
    }

    @RequestMapping("ipSearch")
    public @ResponseBody Map<String, Object> ipSearch(String val) throws IOException {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        List<GoogleIP> ipList=kickGoogleService.ipSearch();
        kickGoogleService.CountUp("googleSearch");
        String finalIP="localhost";
        for(GoogleIP googleIP:ipList){
            boolean status = SocketUtil.socket(googleIP.getIp());
            if (status){
                finalIP=googleIP.getIp();
                kickGoogleService.levelUp(googleIP.getId());
                modelMap.put("final","http://"+finalIP+"/search?q="+val+"&hl=zh-CN");
                return modelMap;
            }
        }
        modelMap.put("final","http://"+finalIP+"/search?q="+val+"&hl=zh-CN");
        return modelMap;
    }

    @RequestMapping("captureContent")
    public @ResponseBody String captureContent() throws IOException {
        GoogleIP googleIP=new GoogleIP();
        String address=null;
        String ip=null;
        Pattern p_html = Pattern.compile("<[^>]+>", Pattern.CASE_INSENSITIVE);
        URL urlmy = new URL("https://github.com/justjavac/Google-IPs/blob/master/README.md#google-%E5%85%A8%E7%90%83-ip-%E5%9C%B0%E5%9D%80%E5%BA%93");
        HttpURLConnection con = (HttpURLConnection) urlmy.openConnection();
        con.setFollowRedirects(true);
        con.setInstanceFollowRedirects(false);
        con.connect();
        BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
        String s = "";
        StringBuffer sb = new StringBuffer("");
        while ((s = br.readLine()) != null) {
            googleIP.setIp("");
            sb.append(s + "\r\n");
            if (s.matches("<th colspan=\"6\">.*?</th>")==true){
                Matcher m_html = p_html.matcher(s);
                address = m_html.replaceAll(""); // 过滤html标签
                googleIP.setAddress(address);
//            System.out.println(address);
            }
            if (s.matches("<td><a href=\".*?\" target=\"_blank\">.*?</a></td>")){
                Matcher m_html = p_html.matcher(s);
                ip = m_html.replaceAll(""); // 过滤html标签
                googleIP.setIp(ip);
//            System.out.println(ip);
            }

            if(StringUtils.isNotBlank(address)&& StringUtils.isNotBlank(googleIP.getIp())){
                googleIP.setDate(new Date());
                int count = kickGoogleService.insertInto(googleIP);
                System.out.println(count);
            }
        }
        return "true";
    }

    public static void main(String[] args) {
        boolean status = SocketUtil.socket("61.19.1.54");
        System.out.println(status);
    }



}